﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scriptor.Presentation.Entity;

namespace Scriptor.Presentation.Templates
{
    public class InterfaceTemplate
    {
        List<string> gUsing { get; set; }
        public StringBuilder clausulaUsingFormatada { get; set; }

        List<string> gInterface { get; set; }
        public StringBuilder clausulaInterfaceFormatada { get; set; }

        List<string> gMethod { get; set; }
        public StringBuilder methodInterfaceFormatado { get; set; }

        public string strUsing()
        {
            gUsing = new List<string>();

            gUsing.Add("using System;");
            gUsing.Add("using System.ServiceModel;");
            gUsing.Add("using System.Collections.Generic;");
            gUsing.Add("using Sinacor.Infra.Service.ExceptionHandling;");
            gUsing.Add("using Sinacor.Infra.Service.Security.Services;");
            gUsing.Add("using Sinacor.Infra.Common;");
            gUsing.Add("using Sinacor.Infra.Common.Exceptions.FaultContracts;");

            clausulaUsingFormatada = new StringBuilder();

            foreach (string str in gUsing)
            {
                clausulaUsingFormatada.Append(str);
                clausulaUsingFormatada.Append(Environment.NewLine);
            }

            return clausulaUsingFormatada.ToString();
        }

        public string strInterface(string pNameSpace, string pInterfaceName, bool showDecorators)
        {
            gInterface = new List<string>();
            
            gInterface.Add("namespace " + pNameSpace);
            gInterface.Add("{");

            if (showDecorators)
            {
                gInterface.Add("\t[ExceptionShielding]");
                gInterface.Add("\t[ServiceContract(Namespace = \"http://www.bovespa.com.br/@@namespace\", ".Replace("@@namespace", pNameSpace) + " Name =\"@@interface-name\", ".Replace("@@interface-name", pInterfaceName) + " SessionMode = SessionMode.Required)]");
            }

            gInterface.Add("\tpublic interface @@interface-name".Replace("@@interface-name", pInterfaceName));
            gInterface.Add("\t{");
            gInterface.Add("@@interface-body");
            gInterface.Add("\t}");
            gInterface.Add("}");

            clausulaInterfaceFormatada = new StringBuilder();

            foreach (string str in gInterface)
            {
                clausulaInterfaceFormatada.Append(str);
                clausulaInterfaceFormatada.Append(Environment.NewLine);
            }

            return clausulaInterfaceFormatada.ToString();
        }

        public string strMethod(Method mtd, bool showDecorators)
        {
            gMethod = new List<string>();

            //Adicionado: 21/8
            if (mtd.MethodComent.Trim().Length > 0)
            {
                gMethod.Add("\t\t/// <summary>");
                gMethod.Add("\t\t///" + mtd.MethodComent.Replace("\n", "\n\t\t///"));
                gMethod.Add("\t\t/// </summary>");
            }
            //Fim Adicionado. phsilva
            if (showDecorators)
            {
                gMethod.Add("\t\t[FaultContract(typeof(InvalidSessionFault))]");
                gMethod.Add("\t\t[FaultContract(typeof(BusinessFault))]");
                gMethod.Add("\t\t[FaultContract(typeof(ValidationFault))]");
                gMethod.Add("\t\t[FaultContract(typeof(GeneralFault))]");
                gMethod.Add("\t\t[FaultContract(typeof(LicensingFault))]");
                gMethod.Add("\t\t[FaultContract(typeof(AccessIsDeniedFault))]");
                gMethod.Add("\t\t[OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = \"@@method-name\")]".Replace("@@method-name", mtd.MethodName));
            }

            methodInterfaceFormatado = new StringBuilder();

            foreach (string str in gMethod)
            {
                methodInterfaceFormatado.Append(str);
                methodInterfaceFormatado.Append(Environment.NewLine);
            }

            methodInterfaceFormatado.Append("\t\t" + mtd.MethodSignature + ";");
            methodInterfaceFormatado.Append(Environment.NewLine);
            methodInterfaceFormatado.Append(Environment.NewLine);

            return methodInterfaceFormatado.ToString();
        }

        public string AppendMethod(string pInterfaceScope, StringBuilder stb)
        {
            return pInterfaceScope.ToString().Replace("@@interface-body", stb.ToString());
        }
    }
}
